package com.example._2025_03_29;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.Arrays;

@RequestMapping("/cookie")
@RestController
public class CookieController {
    /*
        Cookie 是存储在客户端的，所以可以伪造
     */
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response) {
//        原生的方法
//        String name = request.getParameter("username");
//        String age = request.getParameter("age");
//        Integer age1 = Integer.parseInt(age);
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookie信息: ");
        if (cookies != null) {
            for (Cookie cookie : cookies) {
//                推荐这么写(默认的潜规则)
//                字符串常量写在前面, 保证前面的不会为 null
                if ("JavaClass".equals(cookie.getName())) {
                    System.out.println(cookie.getName() + ": " + cookie.getValue());
                }
            }
        }
        assert cookies != null;
        return "获取Cookie信息成功: " + Arrays.toString(cookies);
    }

    /*
        更简单的方式获取 Cookie
     */
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("JavaClass") String JavaClass) {
        System.out.println("JavaClass: " + JavaClass);
        return "获取Cookie成功";
    }

    /*
        设置 Session
     */
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
//        参数为 true ,若 Session 对象不存在，则创建一个; 若对象存在，则直接返回
        HttpSession session = request.getSession(true);
        if (session != null) {
            session.setAttribute("name", "雷军");
            session.setAttribute("studentId", "001");
        }
        return "设置Session成功";
    }

    /*
        获取 Session
     */
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
//        参数为 false, 不存在 session, 返回 null
        HttpSession session = request.getSession(false);
        if (session != null) {
            System.out.println("name" + session.getAttribute("name"));
            System.out.println("studentId" + session.getAttribute("studentId"));
        }
        return "获取session成功";
    }

    /*
        获取 session 的第二种方法
     */
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
//        session = request.getSession();
        System.out.println("name: " + session.getAttribute("name"));
        System.out.println("studentId: " + session.getAttribute("studentId"));
        return "获取session成功";
    }

    /*
        使用注解获取 session
     */
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name", required = false) String name, @SessionAttribute(value = "studentId", required = false) String studentId) {
        System.out.println("name: " + name);
        System.out.println("studentId: " + studentId);
        return "获取session成功";
    }
}
